{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17\n"
     ]
    }
   ],
   "source": [
    "a = 0x10\n",
    "b = 0x01\n",
    "c = a+b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08'\n",
      "b'\\x1f\\x9dm\\xc2\\xcc\\xf3\\xe5\\x9c\\x16\\x00\\x81\\xe6N-\\x88\\x99'\n",
      "AES_CMAC1: 303567f173bc9c9eea609791e4cb7e02\n",
      "<class 'str'>\n",
      "token: 497096bffcfb7e62f538a83cbd5c40cb\n",
      "[73, 112, 150, 191, 252, 251, 126, 98, 245, 56, 168, 60, 189, 92, 64, 203]\n"
     ]
    }
   ],
   "source": [
    "#BE12 安全算法\n",
    "from Crypto.Cipher import AES\n",
    "from Crypto.Hash import CMAC\n",
    "from binascii import hexlify, unhexlify\n",
    "import binascii\n",
    "import os\n",
    "\n",
    "mainkey = unhexlify('01020304050607080102030405060708')\n",
    "print(mainkey)\n",
    "df = unhexlify('1F9D6DC2CCF3E59C160081E64E2D8899')\n",
    "print(df)\n",
    "sk = CMAC.new(mainkey,df,ciphermod=AES)\n",
    "print('AES_CMAC1:',sk.hexdigest())\n",
    "print(type(sk.hexdigest()))\n",
    "\n",
    "sk = unhexlify(sk.hexdigest())\n",
    "\n",
    "seed = unhexlify('FAA79BC20CD6D2CEC54AFA2DBC9B51F6')\n",
    "token = CMAC.new(sk,seed,ciphermod=AES)\n",
    "print('token:',token.hexdigest())\n",
    "token = unhexlify(token.hexdigest())\n",
    "print(list(token))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xfa\\xa7\\x9b\\xc2\\x0c\\xd6\\xd2\\xce\\xc5J\\xfa-\\xbc\\x9bQ\\xf6'\n",
      "[250, 167, 155, 194, 12, 214, 210, 206, 197, 74, 250, 45, 188, 155, 81, 246]\n",
      "bytearray(b'\\xfa\\xa7\\x9b\\xc2\\x0c\\xd6\\xd2\\xce\\xc5J\\xfa-\\xbc\\x9bQ\\xf6')\n",
      "pass\n"
     ]
    }
   ],
   "source": [
    "from binascii import hexlify, unhexlify\n",
    "seed = unhexlify('FAA79BC20CD6D2CEC54AFA2DBC9B51F6')\n",
    "print(seed)\n",
    "print(list(seed))\n",
    "seed1 = [0xFA,0xA7,0x9B,0xC2,0x0C,0xD6,0xD2,0xCE,0xC5,0x4A,0xFA,0x2D,0xBC,0x9B,0x51,0xF6]\n",
    "print(bytearray(seed1))\n",
    "if seed == bytearray(seed1):\n",
    "    print('pass')\n",
    "else:\n",
    "    print('fail')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08'\n",
      "bytearray(b'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08')\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]\n"
     ]
    }
   ],
   "source": [
    "from Crypto.Cipher import AES\n",
    "from Crypto.Hash import CMAC\n",
    "from binascii import hexlify, unhexlify\n",
    "import binascii\n",
    "import os\n",
    "\n",
    "\n",
    "message = unhexlify('01020304050607080102030405060708')\n",
    "print(message)\n",
    "seed = [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8]\n",
    "message2 = bytearray(seed)\n",
    "print(message2)\n",
    "print(list(message2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8c8d564f873fa188ac07860a7bd2f3bce099fa6ec875c0e0b93636ee869604be\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from Crypto.Cipher import AES\n",
    "import hashlib\n",
    "\n",
    "# def pad(text):\n",
    "#     \"\"\"\n",
    "#     #填充函数,使被加密数据的字节码长度是block_size的整数倍\n",
    "#     #AES128-ECB PKCS5填充,补齐到16字节,补充12个0xc\n",
    "#     \"\"\"\n",
    "#     s = ''\n",
    "#     for i in text:\n",
    "#         if i > 15:\n",
    "#             s += hex(i)[2:] # 将seed转化成'6816b4d5'\n",
    "#         else:\n",
    "#             s += '0'\n",
    "#             s += hex(i)[2:]\n",
    "#     print(f's:{s}')\n",
    "#     count = len(text) # seed 固定为4\n",
    "#     add = length - (count % length) # 所以需要填充12个\n",
    "#     # entext = s + (chr(add) * add)\n",
    "#     entext = s + ('0c'*add)\n",
    "#     return entext\n",
    "\n",
    "factor_level1 = 'CCBA77AAE66C46DCF61FDA35A5BB7049'\n",
    "seed = [1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8]\n",
    "\n",
    "factor = factor_level1\n",
    "sha256 = hashlib.sha256(bytes(seed))\n",
    "print(sha256.hexdigest())\n",
    "# seed1 = sha256.hexdigest().upper() + factor  # '1D26...0708'\n",
    "# # logging.info(seed1) # seed1 得到拼接数据48字节\n",
    "# sha256_1 = hashlib.sha256(bytes.fromhex(seed1)) # 通过SHA256对拼接数据运算生成32字节Hash值\n",
    "# seed2 = sha256_1.hexdigest().upper()[:32]  # 截取32字节Hash值的前16字节作为密钥：9A536A10AF8593BD7967D419C42B5EE0\n",
    "# # logging.info(seed2)\n",
    "\n",
    "# length = AES.block_size  # 初始化数据块大小\n",
    "# aes_mode = AES.new(bytes.fromhex(seed2), AES.MODE_ECB)  # 初始化AES,ECB模式的实例\n",
    "\n",
    "# print(pad(seed))\n",
    "# res = aes_mode.encrypt(bytes.fromhex(pad(seed)))  # self.pad(seed)= '6816B4D50C0C0C0C0C0C0C0C0C0C0C0C'  type:str,转换成字节\n",
    "# key = [int(hex(x), 16) for x in res]\n",
    "# print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Private RSA key at 0x11F72E0\n",
      "b'-----BEGIN RSA PRIVATE KEY-----\\nMIIEowIBAAKCAQEAz7oc3qE9ELobSzR/y/P/tdkXOkikj52G0KNuH/9Rivd8VLyY\\ngiKrwEBCdENNpOy/VoaE6R/QmEXKymr/ebKvoFPFFsrysePt5PeRPMh7KKoDaEa4\\nbXcaqCodKjxLdXvv8oHTYpz2lTw8KRVJxncufMb2BKR8yQEeMdXM/0tqGsV0fnLv\\ndeRMvWDNPauvBwGC+ylwZnZ1Vbpi+72NbAYaInyk/cuLBSvhjGmUlyPYTknwojE+\\nFOFlKelI3aWpFjhjlMr6zk6kqjhtIFqyq5a0MfMQfLcxmM6qBEzy+rMWrCqd3Ynn\\nW29N4xbNeKowMDjTpED3SzEHQat4p/bgPf6WeQIDAQABAoIBACGTym3cL1RAWQAX\\njN7JYpig7kXAD0Wdp3ibKkZmzWYCqijCUX6bnxqU6IkFhgv2LtmMJfjO6Tdr9KMy\\nl3gy/3uRfua3yHIjz0T3FZV3LJ0+mg1lfRNIsNjk8dDtbR8g+QDApowmph8qXOkh\\nJUs84ZlOyd6nPfp5wUk574iGxrBr1tOQJhJ+rZsb2Iv4RCFgXTe8O1EAViAOEw0V\\nHeeNJkAI5x2pG7Jy7THy+QJLaGXE5GPcjuA7QUMIb/TarIXzxa9eEG6XMbjYZMsl\\n6Rut+l3dYY34EEGAfZuT6QzQmoWxleqQcBElo6v/PH71mcPYmveFUY3v/Qp4G6cq\\nFhKIG5cCgYEA3XsWFWOPwccpDBbwTPBDkh4dXZs48Bc4qGFWntOG7TFyT/YlIhR4\\nzRXZAGlCvaWvqk1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oHTYpz2lTw8KRVJxncu\\nfMb2BKR8yQEeMdXM/0tqGsV0fnLvdeRMvWDNPauvBwGC+ylwZnZ1Vbpi+72NbAYa\\nInyk/cuLBSvhjGmUlyPYTknwojE+FOFlKelI3aWpFjhjlMr6zk6kqjhtIFqyq5a0\\nMfMQfLcxmM6qBEzy+rMWrCqd3YnnW29N4xbNeKowMDjTpED3SzEHQat4p/bgPf6W\\neQIDAQAB\\n-----END PUBLIC KEY-----'\n",
      "Public RSA key at 0xCE4CEE0\n",
      "<Crypto.Cipher.PKCS1_OAEP.PKCS1OAEP_Cipher object at 0x07773D78>\n",
      "[191, 13, 209, 121, 154, 53, 240, 195, 233, 208, 19, 2, 131, 152, 48, 203, 129, 36, 201, 97, 10, 18, 211, 57, 195, 165, 220, 151, 86, 125, 121, 50, 245, 244, 155, 29, 25, 114, 143, 5, 208, 152, 171, 204, 216, 134, 18, 133, 198, 154, 229, 54, 122, 4, 111, 61, 207, 57, 177, 212, 122, 193, 53, 55, 238, 8, 75, 144, 129, 132, 233, 183, 63, 46, 238, 136, 235, 237, 180, 76, 132, 46, 170, 15, 232, 156, 46, 122, 29, 52, 236, 88, 90, 95, 255, 211, 52, 6, 229, 255, 167, 28, 240, 81, 127, 183, 58, 66, 211, 52, 203, 20, 147, 189, 74, 169, 157, 31, 133, 211, 220, 53, 48, 97, 79, 28, 60, 230, 209, 31, 24, 172, 84, 252, 204, 235, 206, 101, 61, 232, 223, 12, 207, 172, 102, 114, 182, 229, 136, 35, 144, 99, 53, 179, 241, 90, 88, 33, 168, 226, 136, 185, 175, 210, 202, 181, 181, 136, 71, 21, 231, 238, 156, 232, 235, 155, 90, 66, 130, 81, 81, 42, 77, 18, 231, 216, 43, 18, 232, 251, 130, 187, 140, 1, 162, 178, 39, 252, 172, 129, 209, 114, 220, 244, 143, 201, 29, 0, 210, 39, 178, 206, 1, 16, 124, 194, 39, 22, 196, 38, 35, 228, 9, 97, 21, 32, 58, 81, 236, 247, 45, 88, 95, 151, 236, 116, 201, 23, 14, 66, 99, 147, 124, 143, 75, 132, 187, 174, 3, 173, 168, 36, 24, 12, 203, 40]\n",
      "256\n",
      "Private RSA key at 0xCE4CDF0\n",
      "<Crypto.Cipher.PKCS1_OAEP.PKCS1OAEP_Cipher object at 0x07773F40>\n",
      "b'123456'\n"
     ]
    }
   ],
   "source": [
    "#签名 RSA2048\n",
    "from Crypto.PublicKey import RSA\n",
    "\n",
    "key = RSA.generate(2048)\n",
    "private_key = key.export_key()\n",
    "public_key = key.publickey().export_key()\n",
    "print(key)\n",
    "print(private_key)\n",
    "print(public_key)\n",
    "\n",
    "from Crypto.Cipher import PKCS1_OAEP\n",
    "data = b'123456'\n",
    "\n",
    "publickey = RSA.import_key(public_key)\n",
    "print(publickey)\n",
    "cipher = PKCS1_OAEP.new(publickey)\n",
    "print(cipher)\n",
    "encryted_data = cipher.encrypt(data)\n",
    "print(list(encryted_data))\n",
    "print(len(list(encryted_data)))\n",
    "\n",
    "privatekey = RSA.import_key(private_key)\n",
    "print(privatekey)\n",
    "cipher = PKCS1_OAEP.new(privatekey)\n",
    "print(cipher)\n",
    "data = cipher.decrypt(encryted_data)\n",
    "print(data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "512\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "aa = '0D65F93E6115D90492F1FDC312219DAF770529B259A41F965C0ACB4C003A5068404D845DBA37F9EE0F64A0597369CBEA88616FFCB8C9D8E93200DC7990376919CEFB3F484B7570894EA5657D8E080FE9CDC7EA08FF405F91C42826A8ABAABDE6911EE0B2B04E03AC99C369552E730AD530C7A93B67254FB2C1A7C76C5EB1607CDF29BFEDE178ECB3D58EB2055EC7AC90CEC18312867030B20240F476E563AD4098AA658936832D5BD1809268D52D769DD6A40CA2C47FEF9EE449AE114568CFF83BBC883F1CB8E9CC206BCC21882DC4FF7DF162190221401C5412B6B6F8CBD498974809029E31AE6D08EFF357126076860CECB73DF92E49493AAE2F6CDC55AFFB'\n",
    "print(len(aa))\n",
    "bb = '7F2DFE1B7C0101D2F2A1A8319162E7EAAA25BB86D1DE97AE50836C3F27ECFE6C'\n",
    "print(len(bb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sk: <ecdsa.keys.SigningKey object at 0x0147DEE0>\n",
      "vk: VerifyingKey.from_string(b'\\x03r.\\xab\\x00\\x0e\\x95\\x93\\x19\\xe8\\x07@\\xe9\\xaf\\x84\\x06\\xdd\\x87\\xe5\\x0b.\\xa0\\xf3m\\nZC\\x1a~\\x10\\x98\\x86\\x80', NIST256p, sha1)\n",
      "hash_obj: [111, 52, 56, 0, 17, 41, 169, 12, 91, 22, 55, 146, 139, 243, 139, 242, 110, 57, 229, 124, 110, 149, 17, 0, 86, 130, 4, 139, 237, 190, 249, 6]\n",
      "signature: [248, 60, 177, 116, 146, 244, 201, 40, 73, 23, 83, 66, 214, 53, 13, 155, 114, 124, 139, 62, 237, 241, 97, 145, 170, 55, 178, 172, 240, 30, 245, 129, 125, 0, 122, 100, 168, 220, 154, 253, 107, 77, 223, 14, 103, 228, 237, 158, 138, 193, 63, 70, 224, 190, 145, 169, 94, 26, 83, 3, 227, 71, 179, 81]\n",
      "Signature valid: True\n"
     ]
    }
   ],
   "source": [
    "#签名 ECDSA\n",
    "from Crypto.Signature import pss\n",
    "\n",
    "from ecdsa import SigningKey, VerifyingKey, NIST256p\n",
    "import hashlib\n",
    "\n",
    "sk = SigningKey.generate(curve=NIST256p)\n",
    "print('sk:',sk)\n",
    "vk = sk.get_verifying_key()\n",
    "print('vk:',vk)\n",
    "\n",
    "message = b'This is a test message'\n",
    "hash_obj = hashlib.sha256(message).digest()\n",
    "print('hash_obj:',list(hash_obj))\n",
    "signature = sk.sign(hash_obj)\n",
    "print('signature:',list(signature))\n",
    "\n",
    "valid = vk.verify(signature,hash_obj)\n",
    "print('Signature valid:',valid)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
